<?php

namespace common\models\views;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "view_sku_spec".
 *
 * @property int $id
 * @property int $product_id
 * @property int $goods_id
 * @property int $spec_value_id
 * @property string $spec_value
 * @property int $spec_id
 * @property string $spec_name
 * @property int $spec_screen
 * @property string $goods_no
 * @property string $cover
 * @property int $total
 * @property int $shelf
 * @property int $stock
 * @property int $min
 * @property string $price
 * @property string $discount
 * @property string $cost_price
 * @property string $market_price
 * @property string $name
 * @property int $status
 * @property int $deleted_at
 */
class ViewSkuSpec extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'view_sku_spec';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'product_id', 'goods_id', 'spec_value_id', 'spec_id', 'spec_screen', 'total', 'shelf', 'stock', 'min', 'status', 'deleted_at'], 'integer'],
            [['spec_value', 'spec_name', 'goods_no', 'name'], 'required'],
            [['price', 'discount', 'cost_price', 'market_price'], 'number'],
            [['spec_value', 'name'], 'string', 'max' => 50],
            [['spec_name'], 'string', 'max' => 20],
            [['goods_no'], 'string', 'max' => 16],
            [['cover'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'goods_id' => 'Goods ID',
            'spec_value_id' => 'Spec Value ID',
            'spec_value' => 'Spec Value',
            'spec_id' => 'Spec ID',
            'spec_name' => 'Spec Name',
            'spec_screen' => 'Spec Screen',
            'goods_no' => 'Goods No',
            'cover' => 'Cover',
            'total' => 'Total',
            'shelf' => 'Shelf',
            'stock' => 'Stock',
            'min' => 'Min',
            'price' => 'Price',
            'discount' => 'Discount',
            'cost_price' => 'Cost Price',
            'market_price' => 'Market Price',
            'name' => 'Name',
            'status' => 'Status',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * 关联SKU单品信息
     */
    public function getGoods() {
        return $this->hasOne(\common\models\shop\ShopGoods::className(), ['id' => 'goods_id']);
    }

    /**
     * 关联SKU单品信息
     */
    public function getProduct() {
        return $this->hasOne(\common\models\shop\ShopProduct::className(), ['id' => 'product_id']);
    }

    public function getAllowSales() {
        return $this->shelf - $this->total + $this->stock;
    }

    /**
     * 获取商品SKU面板单品展示封面
     */
    public function getThumb() {
        $cover = $this->cover;
        if (!$cover) {
            $spu = $this->product;
            $cover = $spu ? $spu->cover : '';
        }
        return Url::attach($cover);
    }

    public static function sku($id) {
        $query = static::find()->alias('t')->where(['t.product_id' => $id]);
        $query->innerJoinWith([
            'goods g' => function ($query) {
                $query->active(ShopGoods::STATUS_ACTIVE);
            },
        ], false);
        return $query->all();
    }

    public function getTag() {
        return $this->spec_value_id;
    }

    /**
     * 商品会员价格
     * @return string
     */
    public function getMemberPrice() {
        $user = Yii::$app->user->identity;
        $discount = 1;
        if ($user) {
            $discount = $user->discount;
        }
        return number_format($this->price * $discount, 2, '.', '');
    }

    /**
     * 商品市场价格（原价）
     * @return string
     */
    public function getMarketPrice() {
        if ($this->price - $this->memberPrice > 0) {
            return $this->price * 1;
        }
        if ($this->market_price) {
            return $this->market_price * 1;
        }
        return 0;
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id' => 'goods_id',
            'name',
            'product_id',
            'spec_value_id',
            'spec_value',
            'spec_id',
            'spec_name',
            'spec_screen',
            'goods_no',
            'thumb',
            'stock' => 'allowSales',
            'price' => 'memberPrice',
            'min',
            'discount',
            'cost_price',
            'market_price' => 'marketPrice',
        ];
    }

}
